package cn.iocoder.yudao.module.xiaoBizHandler.service.codegen.newInner.yudao.template.java.controller.vo;

import cn.iocoder.yudao.framework.common.genCode.common.StringBuilderHandler;
import cn.iocoder.yudao.module.xiaoBizHandler.dal.dataobject.codegen.XiaoBizHandlerCodegenColumnDO;
import cn.iocoder.yudao.module.xiaoBizHandler.dal.dataobject.codegen.XiaoBizHandlerCodegenTableDO;
import cn.iocoder.yudao.module.xiaoBizHandler.service.codegen.newInner.yudao.handler.CommonColumn;
import cn.iocoder.yudao.module.xiaoBizHandler.service.codegen.newInner.yudao.handler.PathHandler;
import cn.iocoder.yudao.module.xiaoBizHandler.service.codegen.newInner.yudao.template.CodeGenTemplate;
import cn.iocoder.yudao.module.xiaoBizHandler.service.codegen.newInner.GlobalBinding;
import cn.iocoder.yudao.module.xiaoBizHandler.service.codegen.newInner.LocalBinding;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.List;

@Component
public class UpdateReqVO implements CodeGenTemplate {

    @Autowired
    CommonColumn commonColumn;

    public String getPath(XiaoBizHandlerCodegenTableDO table, List<XiaoBizHandlerCodegenColumnDO> columns, GlobalBinding globalBinding, LocalBinding localBinding, XiaoBizHandlerCodegenColumnDO primaryColumn) {
        PathHandler pathHandler = PathHandler.builder()
                .table(table)
                .columns(columns)
                .globalBinding(globalBinding)
                .localBinding(localBinding)
                .primaryColumn(primaryColumn)
                .build();
        return pathHandler.javaModuleImplVOFilePath("UpdateReqVO");
    }


    public String render(XiaoBizHandlerCodegenTableDO table, List<XiaoBizHandlerCodegenColumnDO> columns, GlobalBinding globalBinding, LocalBinding localBinding, XiaoBizHandlerCodegenColumnDO primaryColumn) {
        StringBuilderHandler code = new StringBuilderHandler();
        code
                .apAndBl("package " + globalBinding.getBasePackage() + ".module." + table.getModuleName() + ".controller." + localBinding.getSceneEnum().getBasePackage() + "." + table.getBusinessName() + ".vo;")
                .bl()
                .apAndBl("import lombok.*;")
                .apAndBl("import java.util.*;")
                .apAndBl("import io.swagger.annotations.*;")
                .apAndBl("import javax.validation.constraints.*;")
        ;
        // 处理 Date 字段的引入
        for (XiaoBizHandlerCodegenColumnDO column : columns) {
            if (commonColumn.isCommonColumn(column.getColumnName()))    continue;
            // 时间类型
            if (column.getUpdateOperation() && (!column.getCreateOperation() || !column.getListOperationResult())
                    && "Date".equals(column.getJavaType())) {
                code
                        .apAndBl("import org.springframework.format.annotation.DateTimeFormat;")
                        .bl()
                        .apAndBl("import static " + globalBinding.getDateUtilsClassName() + ".FORMAT_YEAR_MONTH_DAY_HOUR_MINUTE_SECOND;")
                ;
                break;
            }
        }
        code
                .bl()
                .apAndBl("@ApiModel(\"" + localBinding.getSceneEnum().getName() + " - " + table.getClassComment() + "更新 Request VO\")")
                .apAndBl("@Data")
                .apAndBl("@EqualsAndHashCode(callSuper = true)")
                .apAndBl("@ToString(callSuper = true)")
                .apAndBl("public class " + localBinding.getSceneEnum().getPrefixClass() + "" + table.getClassName() + "UpdateReqVO extends " + localBinding.getSceneEnum().getPrefixClass() + "" + table.getClassName() + "BaseVO {")
                .bl()
        ;
        for (XiaoBizHandlerCodegenColumnDO column : columns) {
            if (commonColumn.isCommonColumn(column.getColumnName()))    continue;
            // 不是通用字段
            if (column.getUpdateOperation() && (!column.getCreateOperation() || !column.getListOperationResult())) {
                code.apAndBl(_Column.render(column));
                code.bl();
            }
        }
        code.apAndBl("}");
        code.bl();

        return code.toString();
    }

}